package opc.read.utils;

import org.jinterop.dcom.common.JIException;
import org.openscada.opc.lib.common.ConnectionInformation;
import org.openscada.opc.lib.common.NotConnectedException;
import org.openscada.opc.lib.da.*;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.*;

/**
 * @Description: OpcServe的连接信息，采集信息工具类
 * @author: liyuan
 * @data 2021-01-15 10:45
 */
public class OpcServerUtil {
    public static ConnectionInformation connection() throws IOException {
        Map<String, String> dataSources = ReadHostProt.trueHostProp();
        final ConnectionInformation ci = new ConnectionInformation();
        ci.setHost(dataSources.get("host"));
        ci.setDomain(dataSources.get("domain"));
        ci.setUser(dataSources.get("user"));
        ci.setPassword(dataSources.get("password"));
        ci.setClsid(dataSources.get("clsid"));
        return ci;
    }

    /***
     *
     * @param itemSource  opc信号
     * @param xinhaoSources 文件的信号
     * @param server
     * @throws DuplicateGroupException
     * @throws NotConnectedException
     * @throws JIException
     * @throws UnknownHostException
     * @throws Exception
     */
    public static void fileIn2OpcIn(Map<String, Map<String, Item>> itemSource, LinkedHashMap<String, Object> xinhaoSources, Server server) throws DuplicateGroupException, NotConnectedException, JIException, UnknownHostException,Exception {
        for (Map.Entry<String, Object> groupMap : xinhaoSources.entrySet()) {  // group
            String groupName = groupMap.getKey();
            if (groupName.equals("date")) {
                continue;
            }
            //必须先为null；不知道为啥
            Group group = null;
            group = server.addGroup(groupName);
            group.setActive(true);
            List<String> shiPeiItems = new ArrayList<String>();
            for (String itemName : ((Map<String, Object>) groupMap.getValue()).keySet()) {
                if (itemName.startsWith("PM28_CN.") || itemName.startsWith("PM28_ZQ.") || itemName.startsWith("PM28_BJQS.") || itemName.startsWith("PM28_ZZQS.") || itemName.startsWith("PM28_GAS_Z.")
                        || itemName.startsWith("PM28_FT28878B.") || itemName.startsWith("PM28_JF.") || itemName.startsWith("PM28_GF.") || itemName.startsWith("PM28_DG.")) {
                    itemName = itemName.replace(".", "__");
                    shiPeiItems.add("xianhe.设备 1." + itemName);
                }
                //不变的信号
//                    else if (itemName.equals("PM28_CN") || itemName.equals("PM28_ZQ") || itemName.equals("PM28_BJQS") || itemName.equals("PM28_ZZQS") || itemName.equals("PM28_GAS_Z")
//                            || itemName.equals("PM28_FT28878B") || itemName.equals("PM28_JF") || itemName.equals("PM28_GF") || itemName.equals("PM28_DG")) {
//                        String titemName = itemName.split("\\.")[0];
//                        shiPeiItems.add("xianhe.设备 1." + itemName);
//                    }
                //.Value和.PV需要分割
                else {
                    shiPeiItems.add("xianhe.设备 1." + itemName.split("\\.")[0]);
                }
            }
            String[] items = shiPeiItems.toArray(new String[shiPeiItems.size()]); // 得到信号数组

            Map<String, Item> itemMap = group.addItems(items);
            itemSource.put(groupName, itemMap); // 组，组信号
        }
    }

    public static void fileIn2RealOpcIn(Map<String, Map<String, Item>> itemSource, LinkedHashMap<String, Object> xinhaoSources, Server server) throws DuplicateGroupException, NotConnectedException, JIException, UnknownHostException, AddFailedException {
        for (Map.Entry<String, Object> groupMap : xinhaoSources.entrySet()) {  // group
            String groupName = groupMap.getKey();
            if (groupName.equals("date")) {
                continue;
            }
            //必须先为null；不知道为啥
            Group group = null;
            group = server.addGroup(groupName);
            group.setActive(true);
            /** 得到信号值**/
            Set<String> itemNameS = ((Map<String, Object>) groupMap.getValue()).keySet();
            /***转为字符串数组**/
            String[] items = itemNameS.toArray(new String[itemNameS.size()]); // 得到信号数组
            Map<String, Item> itemMap = group.addItems(items);
            itemSource.put(groupName, itemMap); // 组，组信号
        }
    }
}
